#pragma once
#include<pthread.h>
namespace ly{
    class Mutex {
    public:
        Mutex(const Mutex&) = delete;
        Mutex& operator=(const Mutex&) = delete;
        Mutex() {
            pthread_mutex_init(&mutex, NULL);
        }
        ~Mutex() {
            pthread_mutex_destroy(&mutex);
        }
        void lock() {
            pthread_mutex_lock(&mutex);
        }
        void unlock() {
            pthread_mutex_unlock(&mutex);
        }
        pthread_mutex_t* get_mutex() {
            return &mutex;
        }

    private:
        pthread_mutex_t mutex;
    };
    // 采⽤RAII⻛格，进⾏锁管理 
    class LockGuard{
    public:
        LockGuard(ly::Mutex& mutex):_mutex(mutex){
            _mutex.lock();
        }
        pthread_mutex_t* get_mutex(){
            return _mutex.get_mutex();
        }
        ~LockGuard(){
            _mutex.unlock();
        }
    private:
        ly::Mutex& _mutex; 
    };
}